<template>
  <div class="min-h-screen bg-gray-50 flex flex-col justify-center py-12 sm:px-6 lg:px-8">
    <div class="sm:mx-auto sm:w-full sm:max-w-md">
      <div class="flex items-center justify-center">
        <div class="w-12 h-12 rounded-lg bg-primary flex items-center justify-center">
          <i class="fa fa-cubes text-white text-xl"></i>
        </div>
        <h2 class="ml-3 text-2xl font-extrabold text-gray-900">智能建站系统</h2>
      </div>
      <p class="mt-2 text-center text-sm text-gray-600">
        请登录您的账号以继续
      </p>
    </div>

    <div class="mt-8 sm:mx-auto sm:w-full sm:max-w-md">
      <div class="bg-white py-8 px-4 shadow sm:rounded-lg sm:px-10">
        <form class="space-y-6" @submit.prevent="handleLogin">
          <div>
            <label for="email" class="block text-sm font-medium text-gray-700">邮箱地址</label>
            <div class="mt-1">
              <input id="email" name="email" type="email" v-model="form.email" required class="appearance-none block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-primary focus:border-primary sm:text-sm">
            </div>
          </div>

          <div>
            <label for="password" class="block text-sm font-medium text-gray-700">密码</label>
            <div class="mt-1">
              <input id="password" name="password" type="password" v-model="form.password" required class="appearance-none block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-primary focus:border-primary sm:text-sm">
            </div>
          </div>

          <div class="flex items-center justify-between">
            <div class="flex items-center">
              <input id="remember-me" name="remember-me" type="checkbox" v-model="form.remember" class="h-4 w-4 text-primary focus:ring-primary border-gray-300 rounded">
              <label for="remember-me" class="ml-2 block text-sm text-gray-900">
                记住我
              </label>
            </div>

            <div class="text-sm">
              <a href="#" class="font-medium text-primary hover:text-primary/80">
                忘记密码?
              </a>
            </div>
          </div>

          <div>
            <button type="submit" :disabled="loading" class="w-full flex justify-center py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-primary hover:bg-primary/90 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary disabled:opacity-70">
              <span v-if="loading"><i class="fa fa-spinner fa-spin mr-2"></i>登录中...</span>
              <span v-else>登录</span>
            </button>
          </div>
        </form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { login } from '@/utils/auth'
import { showToast } from '@/utils/toast'

const router = useRouter()
const form = ref({
  email: 'admin@example.com', // 默认填充管理员邮箱
  password: 'admin123', // 默认填充管理员密码
  remember: true
})
const loading = ref(false)

const handleLogin = async () => {
  try {
    // 使用真实的登录API
    await login(form.value)
    // 登录成功后跳转到仪表盘
    router.push('/')
    showToast('登录成功', 'success')
  } catch (error) {
    console.error('登录失败:', error)
    showToast('登录失败，请检查用户名和密码', 'error')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
</style>
